<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use zgldh\QiniuStorage\QiniuStorage;

class UserController extends Controller
{

    const guard = "admin"; //验证模板

    /**
     * 获取auth模板
     * @return Auth
     */
    protected function getGuard()
    {
        return Auth::guard(self::guard);
    }

    /**
     * 用户登录界面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function login(Request $request)
    {
        if ($request->isMethod('post')) {
            if ($this->getGuard()->attempt(['username' => $request->username, 'password' => $request->password])) {
                $user = $this->getGuard()->user();
                if(config('filesystems.default')=="qiniu"){
                    $user->avatar = Storage::disk()->downloadUrl($user->avatar);
                }
                $request->session()->put('admin', $this->getGuard()->user()->toArray());
                return redirect("/admin");
            } else {
                $message = '账号不存在';
            }
        }
        return view('admin.login.index')->with('message', $message ?? "");
    }

    /**
     * 用户退出
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout(Request $request)
    {
        $this->getGuard()->logout();
        $request->session()->forget("admin");
        return redirect("admin/login");
    }

    public function test(Request $request){
        dd(123123);
    }
}